clc;
clear;
%电机参数
Rs = 0.02;
Ls = 74e-6;
%电流环控制频率10k
Ts = 100e-6;
%电流环相关的总延时
Td = 1.5*Ts;

%期望的系统带宽
W=400*2*pi;

%pi控制器参数
Kp = W * Ls;
Ki = W * Rs;

%传递函数
s = tf('s');

%delay
G_delay = exp(-s*Td);

%被控对象的模型
G_plant = tf(1/Ls, [1 Rs/Ls]);

%pi的传递函数
G_pi = Kp * tf([1 Ki/Kp], [1 0]);

%开环传递函数
G_Open = series(G_pi, G_plant);
G_Open_Delay = series(G_Open, G_delay);
%闭环传递函数
G_Close_Delay = feedback(G_Open_Delay, 1, -1);
G_Close = feedback(G_Open, 1, -1);

Beta = W*Td;
Alpha = Beta*(abs(sqrt(sin(Beta)^2+1))-sin(Beta));
Kp_New = Alpha*Ls/Td;
Ki_New = Alpha*Rs/Td;
PID_id_a = tf([Kp_New Ki_New],[1 0]);
G_Close_Delay_new = feedback(series(series(PID_id_a, G_plant),G_delay), 1, -1);

%bode图
figure(1);
bodeplot(G_Close_Delay, '-r', G_Close, '-.b', G_Close_Delay_new, '-.g');
grid on;
%输入阶跃信号的输出信号
figure(2);
step(G_Close_Delay_new, 0.01);
grid on;

bandwidth(G_Close_Delay_new)/2/pi
bandwidth(G_Close_Delay)/2/pi
